﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using bdxyit.com.RecruitmentAppSys.Models;
using bdxyit.com.RecruitmentAppSys.ViewModels;
using System.Diagnostics;
using bdxyit.com.RecruitmentAppSys.BLL;

namespace bdxyit.com.RecruitmentAppSys.Controllers
{
    //[Authorize(Roles = "Manager")]
    //[Authorize(Roles = "NormalUser")]
    public class BasicInfoController : Controller
    {
        //
        // GET: /BasicInfo/
        //基本信息页面初始化数据
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: 
        //只读查看用户信息
        public ActionResult Details(string  userId)
        {
            BasicInfoViewModels basic = BasicInfoViewModelBLL.DetailsBasicInfo(userId);
            return View(basic);
        }

        //
        // GET: /BasicInfoView/Create
        //创建用户信息
        public ActionResult Create(string userId)
        {
            BasicInfoViewModels basic = BasicInfoViewModelBLL.InitBasicInfo(userId);
            return View(basic);
        } 

        //
        // POST: /BasicInfo/Create
        //创建用户信息
        [HttpPost]
        public ActionResult Create(BasicInfoViewModels basicInfo,FormCollection collection)
        {
            try
            {
                string[] formValue = collection["SysID"].Split(new char[] { ',' });
                basicInfo.UserInfo.Sex = formValue[0];
                basicInfo.UserInfo.EthnicID = collection["EthnicID"].ToString();
                basicInfo.UserInfo.PoliticalID = formValue[1];
                basicInfo.UserInfo.LeftVisionID = formValue[2];
                basicInfo.UserInfo.RightVisionID = formValue[3];
                basicInfo.UserInfo.Marital = formValue[4];
                if(ModelState.IsValid)
                {
                  UserInfo ui =  UserInfoBLL.GetBasicInfoByUserID(basicInfo.UserInfo.UserID);
                  if (ui != null)
                  {
                      UserInfoBLL.RemoveBasicInfo(ui.UserID);
                  }
                  else {
                      UserInfoBLL.CreateBasicInfo(basicInfo.UserInfo);
                  }
                }
                return RedirectToAction("Create", "FamilyInfo", new
                 {
                     id = basicInfo.UserInfo.UserID
                 });
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
        
        //
        // GET: 
        public ActionResult Edit(string userId)
        {
            BasicInfoViewModels basic = BasicInfoViewModelBLL.EditBasicInfo(userId);
            return View(basic);
        }

        //
        // POST: 
        [HttpPost]
        public ActionResult Edit(BasicInfoViewModels basicInfo, FormCollection collection)
        {
            try
            {
                string[] formValue = collection["SysID"].Split(new char[] { ',' });
                basicInfo.UserInfo.Sex = formValue[0];
                basicInfo.UserInfo.EthnicID = collection["EthnicID"].ToString();
                basicInfo.UserInfo.PoliticalID = formValue[1];
                basicInfo.UserInfo.LeftVisionID = formValue[2];
                basicInfo.UserInfo.RightVisionID = formValue[3];
                basicInfo.UserInfo.Marital = formValue[4];
                if (ModelState.IsValid)
                {
                    UserInfo ui = UserInfoBLL.GetBasicInfoByUserID(basicInfo.UserInfo.UserID);
                    if (ui != null)
                    {
                        UserInfoBLL.EditBasicInfo(basicInfo.UserInfo);
                    }
                    else
                    {
                        UserInfoBLL.CreateBasicInfo(basicInfo.UserInfo);
                    }
                }
                //未知的return流程
                return RedirectToAction("Index");
                //return RedirectToAction("Create", "FamilyInfo", new
                //{
                //    id = basicInfo.UserInfo.UserID
                //});
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        //
        // GET: 
        public ActionResult Delete(string userid)
        {
            UserInfoBLL.RemoveBasicInfo(userid);
            return View();
        }
    }
}
